package com.kang.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.kang.pojo.AjaxResult;
import com.kang.pojo.Jurisdiction;
import com.kang.pojo.Role_jurisdiction;
import com.kang.service.JurisdictionService;
import com.vonechina.entity.page.RootPage;

/**
 * 权限管理
 * 
 * @author ysq
 *
 */
@Controller
@RequestMapping("Jurisdiction")
@SessionAttributes({ "rootPage" })
public class RottController extends com.kang.controller.Controller {
	/**
	 * 权限服务
	 */
	@Resource
	private JurisdictionService jss;

	/**
	 * 搜索根据条件查询权限jurisdiction数据list
	 * 
	 * @param session
	 * @param model
	 * @param rootPage
	 * @return
	 */
	@RequestMapping("selectByWhere")
	public String PurchaseorderSelectByWhere(HttpSession session, Model model,
			RootPage rootPage) {
		initRight(session);
		if (checkRight("管理角色权限") != SUCCESS) {
			return "basic/noRight";
		}

		List<Jurisdiction> jsm = jss.selstcJurisdiction(rootPage);// 根据条件查询权限jurisdiction数据list
		model.addAttribute("jsm", jsm);

		rootPage.setPageUrl("Jurisdiction/selectByWhere.do");
		model.addAttribute("page", rootPage);
		return "system/root";
	}

	/**
	 * 修改权限
	 * 
	 * @param session
	 * @param model
	 * @param jsc
	 * @return
	 */
	@RequestMapping("updateroot")
	@ResponseBody
	public String updateRott(HttpSession session, Model model, Jurisdiction jsc) {
		initRight(session);
		AjaxResult result = new AjaxResult();
		if ("启用".equals(jsc.getStatus())) {
			if (checkRight("启用权限") != SUCCESS) {
				result.setResult("noRight");
				return JSON.toJSONString(result);
			}
		} else {
			if (checkRight("关闭权限") != SUCCESS) {
				result.setResult("noRight");
				return JSON.toJSONString(result);
			}
		}
		int usercount = jss.updateone(jsc);// 修改权限

		if (usercount > 0) {
			result.setResult("success");
		} else {
			result.setResult("fail");
		}

		return JSON.toJSONString(result);

	}

	/**
	 * 根据角色id查询对用的权限
	 * 
	 * @param model
	 * @param roleid
	 * @param session
	 * @return
	 */
	@RequestMapping("selectJurisdiction")
	public String PurchaseorderJurisdiction(Model model, String roleid,
			HttpSession session) {
		initRight(session);
		model.addAttribute("isEdit", checkRight("管理角色权限") == SUCCESS);
		List<Role_jurisdiction> jurisdiction = jss
				.selectRole_jurisdiction(Integer.parseInt(roleid));
		session.setAttribute("jurisdiction", jurisdiction);
		model.addAttribute("roleid", roleid);
		return "system/role/updaterott";
	}

	/**
	 * 设置角色权限
	 * 
	 * @param session
	 * @param model
	 * @param idlist
	 * @param roleid
	 * @return
	 */
	@RequestMapping("updateroots")
	public String updateRott2(HttpSession session, Model model, String idlist,
			Integer roleid) {
		initRight(session);
		if (checkRight("管理角色权限") != SUCCESS) {
			return "basic/noRight";
		}
		jss.delectones(roleid);// 删除权限
		if (idlist != null && !idlist.equals("")) {
			String[] str = idlist.split(",");
			List<Role_jurisdiction> rolejlist = new ArrayList<Role_jurisdiction>();
			for (int i = 0; i < str.length; i++) {
				if (str[i] != null && !str[i].equals("")) {
					Role_jurisdiction ro_js = new Role_jurisdiction();
					ro_js.setJurisdictionid(Integer.parseInt(str[i]));
					ro_js.setRoleid(roleid);
					ro_js.setCreatedate(new Date());
					ro_js.setStatus("开启");
					// int counts=jss.insertones(ro_js);
					rolejlist.add(ro_js);
				}
			}
			jss.insertones(rolejlist);// 重新插入权限
		}
		return "redirect:../role/selectByWhere.do";
	}
}
